package com.honglang.common.tools;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FileUtil {
	
	public boolean createDirIfNotExist(File file){
		
		if(!file.exists()){
				return file.mkdirs();
		}else{
			return true;
		}
		
		
	}
	
	
	/**
	 * 拷贝文件
	 * @param fromFile
	 * @param toFile
	 * @return
	 */
	public static boolean copyFile(String fromFile,String toFile) {
		      FileInputStream in = null;
		      FileOutputStream out =null;
		        try {  
		            in = new java.io.FileInputStream(fromFile);  
		            out = new FileOutputStream(toFile);  
		            byte[] bt = new byte[1024];  
		            int count;  
		            while ((count = in.read(bt)) > 0) {  
		                out.write(bt, 0, count);  
		            }  
		            in.close();  
		            out.close();  
		            return true;  
		        } catch (IOException ex) {  
		           ex.printStackTrace();
		        	return false;  
		      }finally { 
		    	  if (in != null) {
		    		  try {
						in.close();
		    		  } catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
		    		  } 
		    		}
		    	  if (out != null) {
		    		 try {
						out.close();
		    		 } catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
		    		 } 
		    	  }
	          }
	}  
	
	
	
	/**
	 * 拷贝文件
	 * @param fromFile
	 * @param toFile
	 * @return
	 */
	public static boolean copyFile(File fromFile,File toFile) {
		      FileInputStream in = null;
		      FileOutputStream out =null;
		        try {  
		            in = new java.io.FileInputStream(fromFile);  
		            out = new FileOutputStream(toFile);  
		            byte[] bt = new byte[1024];  
		            int count;  
		            while ((count = in.read(bt)) > 0) {  
		                out.write(bt, 0, count);  
		            }  
		            in.close();  
		            out.close();  
		            return true;  
		        } catch (IOException ex) {  
		           ex.printStackTrace();
		        	return false;  
		      }finally { 
		    	  if (in != null) {
		    		  try {
						in.close();
		    		  } catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
		    		  } 
		    		}
		    	  if (out != null) {
		    		 try {
						out.close();
		    		 } catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
		    		 } 
		    	  }
	          }
	}  
	
	
	/**
	 * 删除一个文件夹
	 * @param Path
	 */
    public static void deleteDirectory(String Path){
    	
       File fileList = new File(Path);
		
		for(File file :fileList.listFiles()){
			
			if (file.isDirectory()){
				FileUtil.deleteDirectory(Path);
			}  else{
					file.delete();
			}
		}
		fileList.delete();
    }
    
    /**
	 * 取得某文件夹下所有的某类文件
	 * @param fileDirectory
	 * @param filter
	 * @return
	 */
	public static List<File> getFilesByTypes(File fileDirectory,FileFilter filter ){
		List<File> fileList = new ArrayList<File>();
		List<File>  pathList = new ArrayList<File>();
		
		FileFilter pathFileter = new FileFilter(){
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		};
		if (fileDirectory.isDirectory()){
			
			pathList.addAll(Arrays.asList(fileDirectory.listFiles(pathFileter)));
			fileList.addAll(Arrays.asList(fileDirectory.listFiles(filter)));
			
			while(!pathList.isEmpty()){
				
				pathList.addAll(Arrays.asList(pathList.get(0).listFiles(pathFileter)));
				fileList.addAll(Arrays.asList(pathList.get(0).listFiles(filter)));
				pathList.remove(0);
			}
		}else{
			
			if(filter.accept(fileDirectory)){
				fileList.add(fileDirectory);
			}
		}
		return fileList;
		
	}
	
	
	/**
	 * 取得某文件夹下所有的某类文件
	 * @param fileDirectory
	 * @param filter
	 * @return
	 */
	public static List<File> getFilesByTypes(File fileDirectory,final String[] fileTypes ){
		List<File> fileList = new ArrayList<File>();
		List<File>  pathList = new ArrayList<File>();
		FileFilter pathFileter = new FileFilter(){
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		};
		FileFilter filter = new FileFilter(){
			public boolean accept(File pathname) {
				String type = pathname.getName().substring(pathname.getName().lastIndexOf(".")+1);
				for(String str : fileTypes){
					if (str.equals(type)){
						return true;
					}
				}
				return false;
			}
			
		};
		if (fileDirectory.isDirectory()){
			
			pathList.addAll(Arrays.asList(fileDirectory.listFiles(pathFileter)));
			fileList.addAll(Arrays.asList(fileDirectory.listFiles(filter)));
			
			while(!pathList.isEmpty()){
				
				pathList.addAll(Arrays.asList(pathList.get(0).listFiles(pathFileter)));
				fileList.addAll(Arrays.asList(pathList.get(0).listFiles(filter)));
				pathList.remove(0);
			}
		}else{
			
			if(filter.accept(fileDirectory)){
				fileList.add(fileDirectory);
			}
		}
		return fileList;
		
	}
	
	
	/**
	 * 拷贝文件夹
	 * @param sourceDir
	 * @param targetDir
	 */
	public void copyDir(String sourceDir ,String targetDir){
		FileFilter fileter = new FileFilter(){

			@Override
			public boolean accept(File pathname) {
				// TODO Auto-generated method stub
				return pathname.isFile();
			}};
		List<File> fileList = FileUtil.getFilesByTypes(new File(sourceDir),fileter);
		
		for (File file : fileList){
			System.out.println(file.getAbsolutePath());
			File tf = new File(file.getAbsolutePath().replace(sourceDir, targetDir));
			File tfParent = new File(tf.getParent());
			if (!tfParent.exists()){
				tfParent.mkdirs();
			}
			FileUtil.copyFile(file.getAbsolutePath(), tf.getAbsolutePath());
			
		}
		
		
	}
	
	/**
	 * 移动文件
	 * @param fromFile
	 * @param toFile
	 */
	public void moveFile(File fromFile,File toFile){
		File pathFile = new File(toFile.getParent());
		if(!pathFile.exists()){
			pathFile.mkdirs();
		}
		fromFile.renameTo(toFile);
	}
	
	
	/**
	 * 移动文件夹
	 * @param sourceDir
	 * @param targetDir
	 */
	public void moveDir(String sourceDir,String targetDir){
		System.out.println(sourceDir);
		FileFilter fileter = new FileFilter(){			
			public boolean accept(File pathname) {
				// TODO Auto-generated method stub
				return pathname.isFile();
		}};
		List<File> fileList = FileUtil.getFilesByTypes(new File(sourceDir),fileter);
		
		System.out.println(fileList);
		for(File file : fileList){
			File tf = new File(file.getAbsolutePath().replace(sourceDir, targetDir));
			this.moveFile(file, tf);
		}
		FileFilter directorFilter = new FileFilter(){			
			public boolean accept(File pathname) {
				// TODO Auto-generated method stub
				return pathname.isDirectory();
		}};
		fileList = FileUtil.getFilesByTypes(new File(sourceDir),directorFilter);
		fileList.add(new File(sourceDir));
		Collections.sort(fileList, new Comparator<File>(){
			public int getFileDepth(File file){
				
				File tempFile = file;
				int count = 0;
				while (null != tempFile.getParentFile()){
					count ++;
					tempFile = tempFile.getParentFile();
				}
				return count;
			}
			@Override
			public int compare(File arg0, File arg1) {
				// TODO Auto-generated method stub
				return this.getFileDepth(arg0) - this.getFileDepth(arg1);
		}});
		
		for(File file :fileList){
			file.deleteOnExit();
		}
	
	}
    
}
